// pages/tree/tree.js

var utils = require("../../utils/util.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    title : "",
    context : "",
  },

  /**
   * 组件的方法列表
   */
  methods: {

    bindInput(e) {
      // 表单双向数据绑定
      var that = this;
      var dataset = e.target.dataset;
      // data-开头的是自定义属性，可以通过dataset获取到，dataset是一个json对象
      var name = dataset.name;
      var value = e.detail.value;
      // 拼接对象属性名，用于为对象属性赋值
      var attributeName = 'dataList.' + name
      that.data[name] = value;
      that.setData({
        [attributeName]: that.data[name]
      });
      // console.log(that.data.goodList);
    },
    submit: function(){
      // console.log(utils.formatTime(new Date()));
      if(wx.getStorageSync('isLogin')){
        wx.request({
          url: 'http://120.79.197.164:9285/barter/post/addPost',
          method: 'POST',
          data: {
            "createTime": utils.formatTime(new Date()),
            "detailContext": this.data.context,
            "easyContext": "string",
            "id": 0,
            "modifyTime": "string",
            "status": 0,
            "title": this.data.title,
            "type": "string",
            "userId": wx.getStorageSync('uuid'),
          },
          header: {
            'content-type': 'application/json'
          },
          success(res) {
            console.log(res.data.code);
            if (res.data.code == 200) {
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                success: function (res) {
                  if (res.confirm) {//这里是点击了确定以后
                    console.log('感谢您的理解')
                  } else {//这里是点击了取消以后
                    console.log('敬请期待哦')
                  }
                }
              })

            }

          }
        });

      }else{
        wx.showModal({
          title: '提示',
          content: '您尚未登录',
          success: function (res) {
            if (res.confirm) {//这里是点击了确定以后
              console.log('感谢您的理解')
            } else {//这里是点击了取消以后
              console.log('敬请期待哦')
            }
          }
        })

      }
      this.clear();
    },

    clear: function(){
      this.setData({
        title : '',
        context : '',
      })

    }
  }
})
